package org.wzq.dao;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.wzq.biz.Section;

import java.util.List;

/**
 * project: spring-mvc-forum
 * ate: 14-9-17
 * author: wzq
 * description: TODO
 */
@Repository
@SuppressWarnings({"unchecked", "rawtypes"})
public class SectionDao {
    @Autowired
    private SessionFactory sessionFactory;

    @Transactional
    public List<Section> findAll() {
        Session session = sessionFactory.getCurrentSession();
        List<Section> list = session.createQuery("from Section").list();

        for (Section section : list) {
            System.out.println(section);
        }
        return list;
    }

    @Transactional
    public void saveOrUpdate(Section section) {
        sessionFactory.getCurrentSession().saveOrUpdate(section);
    }

    //    @Transactional
//    public List<Topic> allTopinInSection(String sectionId) {
//        String hql = "from Topic where sectionId=" + sectionId;
//        List<Topic> list = sessionFactory.getCurrentSession().createQuery(hql).list();
//        for (Topic topic : list) {
//            System.out.println("aUser = " + topic);
//        }
//        return list;
//    }
//

    @Transactional
    public Section getSectionById(String sectionId) {
        String hql = "from Section where sectionId=?";
        Query query = sessionFactory.getCurrentSession().createQuery(hql);
        query.setString(0, sectionId);
        return (Section) query.uniqueResult();
    }

//    @Transactional
//    public Section getSectionByName(String name) {
//        String hql = "from Section where name=?";
//        Query query = sessionFactory.getCurrentSession().createQuery(hql);
//        query.setString(0, name);
//        return (Section) query.uniqueResult();
//    }
}
